﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.DataAccess;
using University.Workshop.Model;
using System.Windows.Forms;
using System.Globalization;

namespace University.Workshop.Business
{
    public static class RoomStuffManager
    {


        public static bool CreateNewRoom(Room room)
        {
            RoomDataAccess roomdal = new RoomDataAccess();
            roomdal.Dispose();
            if (roomdal.ExistingRoom(room) == false)
            {
                roomdal.NewRoom(room);
                return true;
            }
            else
            {
                return false;

            }
        }

        public static int GetByName(String name)
        {
            RoomDataAccess roomdal = new RoomDataAccess();
            roomdal.Dispose();
            return roomdal.GetByName(name);
        }

        public static void GetAllRooms(DataGridView rooms)
        {
            RoomDataAccess roomdal = new RoomDataAccess();
            roomdal.Dispose();
            roomdal.GetAllRooms(rooms);

        }
        public static int GetIdRoomByName(String name)
        {
            RoomDataAccess roomdal = new RoomDataAccess();
            roomdal.Dispose();
            return roomdal.GetIdRoomByName(name);
        }

        public static void GetAllBookedAndTokenRoomsForADay(DateTime dateTime, DataGridView roomsDataGrid, int idType)
        {
            DrawColumnsHeaderForADay(dateTime, roomsDataGrid);
            RoomDataAccess roomDal = new RoomDataAccess();
            roomDal.Dispose();
            roomDal.GetAllRoomsName(roomsDataGrid, idType);
            //GetStateOfAllRoomsInADay(dateTime, roomsDataGrid, idType);
            roomDal.DrawStates(dateTime, roomsDataGrid, idType, 1);
        }

        public static void GetAllBookedAndTokenRoomsForAWeek(DateTime dateTime, DataGridView roomsDataGrid, int idType)
        {
            DrawColumnsHeader(dateTime, roomsDataGrid);
            RoomDataAccess roomDal = new RoomDataAccess();
            roomDal.Dispose();
            roomDal.GetAllRoomsName(roomsDataGrid, idType);
            GetStateOfAllRoomsInADay(dateTime, roomsDataGrid, idType);
        }

        private static void GetStateOfAllRoomsInADay(DateTime dateTime, DataGridView roomsDataGrid, int idType)
        {
            RoomDataAccess roomDal = new RoomDataAccess();
            roomDal.Dispose();
            roomDal.DrawStates(dateTime, roomsDataGrid, idType, 7);
        }

        public static void DrawColumnsHeaderForADay(DateTime dateTime, DataGridView roomsDataGrid)
        {
            if (roomsDataGrid == null) { throw new ArgumentException("Invalid Input Parameters"); }
            DataGridViewTextBoxColumn rowHeader = new DataGridViewTextBoxColumn();
            rowHeader.Dispose();
            DataGridViewTextBoxColumn day1 = new DataGridViewTextBoxColumn();
            day1.Dispose();
            rowHeader.Name = "Rooms";
            day1.Name = dateTime.ToShortDateString().Substring(0, 5);
            roomsDataGrid.Columns.AddRange(new DataGridViewColumn[] { rowHeader, day1 });
        }
        public static void DrawColumnsHeader(DateTime dateTime, DataGridView roomsDataGrid)
        {
            if (roomsDataGrid == null) { throw new ArgumentException("Invalid Input Parameters"); }
            DataGridViewTextBoxColumn rowHeader = new DataGridViewTextBoxColumn();
            rowHeader.Dispose();
            DataGridViewTextBoxColumn day1 = new DataGridViewTextBoxColumn();
            day1.Dispose();
            DataGridViewTextBoxColumn day2 = new DataGridViewTextBoxColumn();
            day2.Dispose();
            DataGridViewTextBoxColumn day3 = new DataGridViewTextBoxColumn();
            day3.Dispose();
            DataGridViewTextBoxColumn day4 = new DataGridViewTextBoxColumn();
            day4.Dispose();
            DataGridViewTextBoxColumn day5 = new DataGridViewTextBoxColumn();
            day5.Dispose();
            DataGridViewTextBoxColumn day6 = new DataGridViewTextBoxColumn();
            day6.Dispose();
            DataGridViewTextBoxColumn day7 = new DataGridViewTextBoxColumn();
            day7.Dispose();
            rowHeader.Name = "Rooms";
            String[] listName = CreateColumnsName(dateTime);
            day1.Name = listName[0];
            day2.Name = listName[1];
            day3.Name = listName[2];
            day4.Name = listName[3];
            day5.Name = listName[4];
            day6.Name = listName[5];
            day7.Name = listName[6];

            roomsDataGrid.Columns.AddRange(new DataGridViewColumn[] { rowHeader, day1, day2, day3, day4, day5, day6, day7 });
        }

        public static String[] CreateColumnsName(DateTime dateTime)
        {
            String[] list = new String[7];
            for (int iterator = 0; iterator < 7; iterator++)
            {
                list[iterator] = dateTime.AddDays(iterator).ToShortDateString().Substring(0, 5);
            }
            return list;
        }

        public static void GetAllTakenRooms(DateTime dateTime, DataGridView roomsDataGrid)
        {
            RoomDataAccess roomDal = new RoomDataAccess();
            roomDal.Dispose();
            roomDal.GetTakenRooms(dateTime, roomsDataGrid);
        }
        public static void GetAllTakenByName(DateTime dateTime,DataGridView roomsDataGrid,string name)
 	    {
 	        RoomDataAccess roomDal = new RoomDataAccess (); 	
            roomDal.Dispose(); 	
            roomDal.GetTakenRoomsByName(dateTime, roomsDataGrid,name); 	
        }

        public static string GetRoomNameByRoomId(string roomId) {
            int roomID;
            try
            {
                roomID = Convert.ToInt32(roomId, CultureInfo.InvariantCulture);
            }
            catch
            {

                throw new ArgumentException("The Format roomId is invalid ");
            }
            RoomDataAccess roomDal = new RoomDataAccess();
            roomDal.Dispose();
            String roomName =roomDal.GetRoomNameById(roomID);
            return roomName;
        }
    }
}